1747A - Two Groups - CodeForces Solution


constructive algorithms greedy

Please click on ads to support us..

Python Code:

def solve():
	n = int(input(''))
	a = [int(x) for x in input('').split(' ')]
	p, n = 0,0
	for i in a:
		if i > 0:
			p+=i
		if i<=0:
			n+=i
	print(max(abs(p) - abs(n), abs(n)-abs(p)))

t = int(input(''))

for i in range(t):
	solve()

C++ Code:

#include <bits\stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define ull unsigned long long

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t;
    cin >> t;
    while(t--){
        ll n;
        cin >> n;
        ll a[n + 1], ans = 0;
        for(int i = 1; i <= n; i++){
            cin >> a[i];
            ans += a[i];
        }
        cout << abs(ans) << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why